<?php

namespace App\Console\Commands;

use App\Enums\AssetStatus;
use App\Models\Asset;
use App\Services\WalletService;
use Illuminate\Console\Command;

class AssetRecoveryCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'asset:recovery';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '资产回收';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(WalletService $walletService)
    {
        $assets = Asset::query()->where('asset_status', AssetStatus::WAIT_RECOVERY)
            ->limit(10)->get();
        foreach ($assets as $asset) {
            $walletService->transfer(
                $asset->user->mobile,
                $asset->contract_address,
                [$asset->token_id],
                config('services.wallet.recovery_address')
            );
            $asset->asset_status = AssetStatus::RECOVERED;
            $asset->save();
            $this->output->writeln("资产[{$asset->token_id}]已回收");
            sleep(1); // 间隔1s请求，防止钱包服务器负载过大
        }

        return Command::SUCCESS;
    }
}
